package com.example.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO
 *
 * @author ding
 * @since 2024/5/31
 */
@Configuration(proxyBeanMethods = false)
public class MybatisPlusConfig {
    /**
     * 添加分页插件和乐观锁
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        //分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        //数据库类型  MySQL limit ?,?
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        //溢出总页数是否进行处理 false：不好处理
        paginationInnerInterceptor.setOverflow(false);
        //分页条数限制 -1：不限制
        paginationInnerInterceptor.setMaxLimit(-1l);

        //乐观锁
        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();

        interceptor.addInnerInterceptor(paginationInnerInterceptor);   //如果配置多个插件，切记分页最后添加
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
        return interceptor;
    }
}
